<?
/// @brief Define Core class 
/// @author ggugi
/// @date 2012-07-03
/// @params 
/// @return 
class core
{
	static public $model;
    static public $db;
	static public $oParam;
	static protected $sActivity;
	static public $sGroup;
	static public $sType;
	static public $sMethod;
	static public $sFormat;
	static protected $sClassPath;
	static protected $bUseLayout = true;
    static public $sJsonFile;
    static public $bIsCache;
    static public $iCacheExpireTime;
    static public $oJsonData;
    static public $oResult;
	
	/**
	 * @brief main class 
	 **/
	public function __construct()
	{
		if( !is_object(self::$oParam) ) {
			self::setParams();
            self::doException();
            if(self::$sGroup == "admin") return false;
            self::parseJson();
            self::checkParams();
		}
	}

	/**
	 * @brief main class 
	 **/
	public function __destruct()
	{
		
	}

	/**
	 * @brief class
	 * @date 2012-07-03
     * @ect http://domain/index.php?_activity=service|addon&_group=그룹명&_type=execute|page&_method=메소드명
	 **/
	public static function setParams()
	{
        foreach($_REQUEST as $key => $val){
            if(in_array($key, array('_activity', '_group', '_type', '_method'))) continue;
            self::$oParam[$key] = $val;
        }

		self::$sActivity = $_REQUEST['_activity']; 
		self::$sGroup = $_REQUEST['_group']; 
		self::$sType = $_REQUEST['_type']; 
		self::$sMethod = $_REQUEST['_method']; 
        self::$sFormat = empty($_REQUEST['_format']) ? self::$sType : $_RESOURCE['_format']; 

		self::$sClassPath = _APPLICATION."/";
		self::$sClassPath .= implode( "/", array(self::$sActivity, self::$sGroup, self::$sType, self::$sMethod));
		self::$sClassPath .= ".php";
	}

    /**
	 * @brief class
	 * @date 2012-07-03
	 **/
	private function parseJson()
	{
        $sJsonDir = _RESOURCE."/json/".self::$sGroup;
        self::$sJsonFile = $sJsonDir."/".self::$sMethod.".json";
        self::$oJsonData = helper('json') -> parse_json_file(self::$sJsonFile);
        self::$bIsCache =  self::$oJsonData['INFORMATION']['is_cache'];
        self::$iCacheExpireTime =  self::$oJsonData['INFORMATION']['cache_time'];
    }

	/**
	 * @brief class
	 * @date 2012-07-03
     * @ect http://domain/index.php?_activity=service|addon&_group=그룹명&_type=execute|page&_method=메소드명
	 **/
	public static function checkParams()
	{
        foreach(self::$oJsonData['PARAMETER'] as $key => $val){
            if(self::$sGroup == "admin") continue;
            if($val['required'] != 1 && $val['required'] != "true") continue;
            if(empty(self::$oParam) || !array_key_exists($key, self::$oParam)){
                if(IS_TEST_MODE){ 
                    v(self::$oJsonData['PARAMETER']);
                }
                self::doError(101); 
            }
        }
    }

	/**
	 * @brief class
	 * @date 2012-07-03
	 **/
	private function doException()
	{
		if(empty(self::$sGroup)) self::doError(102);
        if(self::$sGroup == "debug"){
            header("Content-Type: text/html; charset=UTF-8");
            require_once(_RESOURCE."/template/debug.php");
            die();
        }

        if(empty(self::$sMethod)){
            $sGroupPath = _APPLICATION."/";
		    $sGroupPath .= implode( "/", array(self::$sActivity, self::$sGroup, self::$sType));
            exec("ls ".$sGroupPath, $aMethod);
            header("Content-Type: text/html; charset=UTF-8");
            
            require_once(_RESOURCE."/template/test.php");
            foreach($aMethod as $val){
                if($val == "common.php") continue;
                $aUri = explode(".php",$val);
                echo sprintf("<a href='/%s/%s' target='_blank'>%s</a></br>", self::$sGroup, $aUri[0], $aUri[0]);
            }
            die();
        }
    }

	/**
	 * @brief class
	 * @date 2012-07-03
	 **/
	public static function setModel()
	{
		$sModelDir = _APPLICATION."/".implode( "/", array(self::$sActivity, self::$sGroup, 'model'));
		require_once($sModelDir."/get.php");
		require_once($sModelDir."/set.php");
        $sModelClassPrefixName = implode("_", array(self::$sGroup, 'model'));
		$sModelGet = $sModelClassPrefixName."_get";
		$sModelSet = $sModelClassPrefixName."_set";
		self::$model->get = new $sModelGet;
		self::$model->set = new $sModelSet;

		return self::$model;
	}

	/**
	 * @brief class
	 * @date 2012-07-03
	 **/
	public static function doError($iErrorNo)
	{
        $aError = parse_ini_file(_FRAMEWORK."/conf/conf_error.ini"); 
        $oResult = array("Result" => 0, "ErrorNo" => $iErrorNo, "ErrorMsg" => $aError[$iErrorNo]);
        die(json_encode($oResult));
	}


}
